const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base.js");
const path = require("path");
const webpack = require("webpack");

const DEV_SERVER_CONFIG = {
  HOST: "127.0.0.1",
  PORT: 9002,
  TIMEOUT: 10000,
  HMR_PATH: "__webpack_hmr",
};

Object.keys(baseConfig.entry).forEach((entryName) => {
  if (entryName !== "vender") {
    baseConfig.entry[entryName] = [
      `webpack-hot-middleware/client?path=http://${DEV_SERVER_CONFIG.HOST}:${DEV_SERVER_CONFIG.PORT}/${DEV_SERVER_CONFIG.HMR_PATH}?timeout=${DEV_SERVER_CONFIG.TIMEOUT}`,
      baseConfig.entry[entryName],
    ];
  }
});

const webpackDevConfig = merge(baseConfig, {
  mode: "development",
  devtool: 'eval-cheap-module-source-map',
  output: {
    filename: "js/[name].[contenthash:8].bundle.js",
    path: path.resolve(process.cwd(), "./app/public/dist/dev/"),
    publicPath: `http://${DEV_SERVER_CONFIG.HOST}:${DEV_SERVER_CONFIG.PORT}/public/dist/dev`,
    globalObject: "this",
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin({
      multiStep: false,
    }),
  ],
});

module.exports = { webpackDevConfig, DEV_SERVER_CONFIG };
